package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.dllo.findfood.adpter.MagzineRecyclerAdpter;
import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.entity.MagzineJson;
import com.lanou.dllo.findfood.entity.MagzineResult;
import com.lanou.dllo.findfood.dialog.BookProgressDialog;
import com.lanou.dllo.findfood.util.Booklister;
import com.lanou.dllo.findfood.volley.VolleySington;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dllo on 15/10/28.
 */
public class MagazineActivity extends Activity implements Booklister{
   private RecyclerView recyclerView;
    private MagzineRecyclerAdpter adpter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_magzine);
        initView();
        getBook();
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.bookRecyclerView);
        GridLayoutManager m = new GridLayoutManager(this,4);
        recyclerView.setLayoutManager(m);
        adpter = new MagzineRecyclerAdpter();
        recyclerView.setAdapter(adpter);
        adpter.setLister(this);
    }

   public void getBook(){
       final BookProgressDialog bookProgressDialo = new BookProgressDialog(this,R.id.animtionImg);
       bookProgressDialo.show();

       String httpUrl = "http://apis.baidu.com/tngou/book/list";
       String httpArg = "id=5&page=1&rows=20";

       String path = httpUrl + "?" + httpArg;

       StringRequest reqest = new StringRequest(path, new Response.Listener<String>() {
           @Override
           public void onResponse(String response) {
                Gson g = new Gson();
               Log.i("lnaou","88888888888" + response);

               MagzineJson result = g.fromJson(response,MagzineJson.class);
               if (result != null){
                   List<MagzineResult> book = result.getTngou();
                   adpter.addData(book);
                   bookProgressDialo.dismiss();
               }




           }
       }, new Response.ErrorListener() {
           @Override
           public void onErrorResponse(VolleyError error) {
               error.printStackTrace();
           }
       }){
           @Override
           //在StringRespset结束之前写{}在大括号里面写 getHeaders() 自动重写方法
           //用Map方法键值对形势添加
           //添加请求头
           public Map<String, String> getHeaders() throws AuthFailureError {
               Map<String, String> map = new HashMap<String, String>();
               map.put("apikey", "7df89e2ed7c7b92e1a2fe5ec8036d0cd");
               return map;
           }
       };

       VolleySington.getInstance().addRequest(reqest);

    }
    @Override
    public void bookClick(MagzineResult bookList) {
        Intent intent = new Intent(this,BookDesptionActivity.class);
        intent.putExtra("id", bookList.getId());
        startActivity(intent);




    }
}
